from freedom import selectlist
from freedom import selecttablelist
import re

def getoptions(name, value=''):
    return selectlist.getoptions(name, value)

def get_table_options(table, valueproperty, textproperty, value=''):
    return selecttablelist.get_table_options(table, valueproperty, textproperty, value)

def getoptions_text(name):
    return selectlist.getoptions_text(name)

def getoptions_singletext(name, value):
    return selectlist.getoptions_singletext(name, value)


def getAdminJsPath():
    return '/static/admin/js'

def getUEditorPath():
    return '/static/ueditor'

def entertobr(text):
    return text.replace('\r\n', '<br/>')

def date(date):
    if type(date) == type(''):
        from .freedom.dateutil.parser import parse
        import datetime
        return datetime.datetime.strftime(parse(text),'%Y-%m-%d')
    else:
        return date.strftime('%Y-%m-%d')

def datetime(date):
    if type(date) == type(''):
        from .freedom.dateutil.parser import parse
        import datetime
        return datetime.datetime.strftime(parse(text),'%Y-%m-%d %H:%M:%S')
    else:
        return date.strftime('%Y-%m-%d %H:%M:%S')

def cutlength(length,suffix=''):
    def cutlength_inside(text):
        regex_juyanmark = re.compile('\|juyan[\s\S]+?\d+?\|')
        juyanmark = regex_juyanmark.findall(text)
        if len(juyanmark) > 0:
            return text[0:min(len(juyanmark[0])+length, len(text)-len(juyanmark[0]))] + suffix
        else:
            return text[0:length] + suffix

    return cutlength_inside

def is_user_login(request):
    from pyramid.security import authenticated_userid
    return authenticated_userid(request) == 'role:user'
